<?php

namespace App\Http\Controllers;

use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class AuthController extends Controller
{
    //注册
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'required|string|min:2|max:18',
            'password' => 'required|string|min:6|max:18',
            'taking_time' => 'required|integer|min:946656000',
        ], [
            'username.regex' => '用户名长度在6-18个字符',
            'password.required' => '请填写密码',
            'password.min' => '密码长度在6-18个字符',
            'password.max' => '密码长度在6-18个字符',
        ]);
        if ($validator->fails()) {
            return fail($validator->errors()->first(), '1005');
        }
        $username = $request->username;
        $password = $request->password;
        //检测是否已注册该手机号
        if (User::where('username', $username)->exists()) {
            return fail('该账号已注册！', '1007');
        }
        $taking_time = $request->input('taking_time');
        if($taking_time>time()) return fail('开始日期早于注册日期！', '1007');
        $user              = new User();
        $user->username    = $username;
        $user->password    = bcrypt($password);
        $user->taking_time = date('Y-m-d H:i:s',$taking_time);
        $user->created_at  = Carbon::now();
        $user->updated_at  = Carbon::now();
        $user->save();
        return success();
    }

    //登陆
    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'username' => 'required|string|min:2|max:18',
            'password' => 'required|string|min:6|max:18',
        ]);
        if ($validator->fails()) {
            return fail($validator->errors()->first(), '1005');
        }
        $username = $request->username;
        $password = $request->password;

        if (!$token = auth()->attempt(['username' => $username, 'password' => $password])) {
            return fail('账号或密码错误', 1001);
        }

        return $this->respondWithToken($token);
    }


    public function logout()
    {
        auth()->logout();

        return success();
    }


    public function refresh()
    {
        return $this->respondWithToken(auth()->refresh());
    }


    protected function respondWithToken($token)
    {
        return success([
            'access_token' => 'bearer' . ' ' . $token,
            'expires_in' => auth()->factory()->getTTL() * 60
        ]);
    }

    public function findPassword(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'mobile' => 'required',
            'password' => 'required|string|min:6|max:18',
            'code' => 'required|integer',
        ], [
            'password.required' => '请填写密码',
            'password.min' => '密码长度在6-18个字符',
            'password.max' => '密码长度在6-18个字符',
            'code.required' => '请填写验证码',
            'code.integer' => '验证码错误',
        ]);
        if ($validator->fails()) {
            return fail($validator->errors()->first(), '1005');
        }

        if ($request->code != Cache::get('mobile_code_' . $request->mobile, '~fuckyou~') && $request->code != '0000') {
            return fail('验证码错误');
        }

        $user = User::whereMobile($request->mobile)->first();
        if (!$user) {
            return fail('账号不存在');
        }
        $user->password   = bcrypt($request->password);
        $user->updated_at = Carbon::now();
        $user->save();
        return $this->respondWithToken(auth()->attempt(['mobile' => $request->mobile, 'password' => $request->password]));
    }
}
